
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )


include( "shared.lua" )




function GM:Initialize( )

	self.BaseClass:Initialize( )
	GAMEMODE:SetupTeams()	
	GameInfo.GameOver = false	
end


function GM:PlayerDisconnected(ply)
end


function GM:ChooseTeam( ply )	
	ply:SetTeam(TEAM_NOTIT)
end


function GM:PlayerSpawn( ply ) 
	GAMEMODE:SetupPlayer( ply )	
	self.BaseClass:PlayerSpawn( pl )
end


function GM:SetupPlayer( ply )	
	ply:RemoveAllAmmo()
	ply:Give( "weapon_crowbar" )

	// set to default weapon
	local defaultweapon = ply:GetInfo( "cl_defaultweapon" )	
	if ( ply:HasWeapon( defaultweapon )  ) then
		ply:SelectWeapon( defaultweapon ) 
	end
	
	ply:SetModel( SKIN_NOTIT )	
	
	if GetNumberOfPlayerOnTeamAny() < 1 then
		ply:SetTeam( TEAM_NOTIT )
		PickANewIt()
	else
		ply:SetTeam( TEAM_NOTIT )
	end	
end


function GetNumberOfPlayerOnTeamAny( )
	local count = 0
	for k,v in pairs(player.GetAll()) do
		if v:Team(team) == TEAM_NOTIT or v:Team(team) == TEAM_NOTIT_F or v:Team(team) == TEAM_IT then
			count = count + 1
		end
	end
	return count
end


function GetNumberOfPlayerOnOnlyNotIt( )
	local count = 0
	for k,v in pairs(player.GetAll()) do
		if v:Team(team) == TEAM_NOTIT then
			count = count + 1
		end
	end
	return count
 end


function GetNumberOfIts( )
	local count = 0
	for k,v in pairs(player.GetAll()) do
		if v:Team(team) == TEAM_IT then
			count = count + 1
		end
	end
	return count
end


function GM:ScalePlayerDamage( ply, group, dmginfo )
	dmginfo:SetDamage (0) 
	if ply:Team() == TEAM_IT then
		dmginfo:SubtractDamage(24) 
	else
		dmginfo:SubtractDamage(100) 
	end 
	
	updateFreeze(ply, dmginfo:GetAttacker() )

	return dmginfo
end

function GM:EntityTakeDamage( ent, inflictor, attacker, amount, dmginfo )
	
	if ent:IsPlayer() and dmginfo:IsFallDamage() then 
		dmginfo:ScaleDamage( 0 ) // no fall damage
	end 
	
	if ent:IsPlayer() == false then return false end
	if attacker:IsPlayer() == false then return false end
	
	if dmginfo:IsExplosionDamage()  then
		dmginfo:ScaleDamage( 0 ) 
	end
	
end

function GM:PlayerDeathThink( ply )
	if ply:Team() == TEAM_IT then
		UnFreezeAll()
		WinDetected( TEAM_NOTIT )
	end
	ply:Spawn() 
end

function GM:GetFrozenPlayers(thisteam)
end

function GM:DoPlayerDeath(ply, attacker, dmginfo)		
end


function UnFreezeAll()
	for k,v in pairs(player.GetAll()) do
		v:Freeze( false ) 
		UpdatePlayerSkin( v , false )
	end
end
concommand.Add( "UnFreezeAll", UnFreezeAll )  


function updateFreeze(ply, attacker)
	if attacker:IsPlayer() then
		if attacker:Team() == TEAM_IT then
			ply:Freeze( true ) 
			UpdatePlayerSkin( ply , true)
		else
			ply:Freeze( false ) 
			UpdatePlayerSkin( ply , false)
		end
	end
	
	if GetNumberOfPlayerOnOnlyNotIt() < 1 then
		WinDetected( TEAM_IT )
	end
	
	return false
end


function GM:CanPlayerSuicide ( ply )
		ply:PrintMessage(HUD_PRINTTALK, "You can't kill yourself!!!")
		return false
end 


function PickANewIt()

	WIN = 0

	//'set everyone not it'	
	for k,v in pairs(player.GetAll()) do
		v:RemoveAllAmmo()
		v:Freeze( false ) 
		v:SetTeam(TEAM_NOTIT)
		UpdatePlayerSkin(v, false)
	end

	//'pick a new it'
	local newItply = ChooseRandom( player.GetAll() ) 
	newItply:SetTeam(TEAM_IT)

	newItply:GiveAmmo( 15,	"Grenade", 		true )	
	newItply:Give( "weapon_frag" )
	newItply:SetModel( SKIN_IT ) 
	
	//setup counters and timers
	ResetCounters()	
	if timer.IsTimer("GameModeTick") then 
		timer.Destroy("GameModeTick")
	end 
	timer.Create( "GameModeTick", 1, 0, GameModeTick ) 	
	
	return 
end
concommand.Add( "PickANewIt", PickANewIt )  





function ResetCounters()
	TIME_FREEZE_BOMB = TIME_FREEZE_BOMB_MAX
	TIME_ROUND = TIME_ROUND_MAX
end 


function GameModeTick()
	if TIME_ROUND > 0 then
		TIME_ROUND = TIME_ROUND - 1
	else
		WinDetected( TEAM_NOTIT )
		return 
	end
	
	if TIME_FREEZE_BOMB > 0 then
		TIME_FREEZE_BOMB = TIME_FREEZE_BOMB - 1
	end
	
	
	SetGlobalInt( "TIME_FREEZE_BOMB", TIME_FREEZE_BOMB)
	SetGlobalInt( "TIME_ROUND", TIME_ROUND)
	SetGlobalInt( "WIN", WIN )
	if GetNumberOfIts() != 1 then
		PickANewIt()
	end
end


function WinDetected( t )
	
        for k,v in pairs(player.GetAll()) do
                if v:Team(team) == t then
			v:SetFrags( v:Frags() + 1 )	
                end
        end


	team.AddScore (t, 1) 
	WIN = t
	SetGlobalInt( "WIN", WIN)
	timer.Remove( "GameModeTick" )
	timer.Simple(5, PickANewIt)
	return
end







